<template>
  <addAppView ref="addAppView" />
  <el-main> 
    <div class="app-table">
      <div class="app-body">
        <div class="app-search">
          <el-form :inline="true" class="demo-form-inline" v-model="form">
            <el-form-item label="应用名称">
              <el-input placeholder="请输入应用名称" v-model="form.appName"></el-input>
            </el-form-item>
            <el-form-item label="应用类型">
              <el-select placeholder="请选择应用类型" v-model="form.type" clearable style="width: 200px;">
                <el-option label=" PC应用" value="1"></el-option>
                <el-option label="手机应用" value="2"></el-option>
                <!-- <el-option label="基础平台菜单" value="3"></el-option> -->
              </el-select>
            </el-form-item> 
            <el-form-item label="用户群体">
              <el-select placeholder="请选择用户群体" v-model="form.userType" clearable style="width: 200px;">
                <el-option label="学生" value="1"></el-option>
                <el-option label="人员" value="2"></el-option>
                <!-- <el-option label="基础平台菜单" value="3"></el-option> -->
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button class="serach_btn" plain type="primary" @click="onSubmit">搜索</el-button>
              <el-button class="add_btn" plain type="success" @click="add">添加</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-table :data="list" style="width: 100%" size="default" :row-class-name="tableRowClassName">
          <!-- <el-table-column prop="id" label="id" width="60" /> -->
          <el-table-column prop="appName" label="应用名称" width="140" />
          <el-table-column prop="icon" label="应用图标" width="90">
            <template style="display: block" #default="scope">
              <el-image :src="scope.row.icon" class="index-image" :fit="fit">
                <template #error>
                  <div class="image-slot">
                    <el-icon class="index-image">
                      <IconPicture />
                    </el-icon>
                  </div>
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="userType" label="用户群体" width="80">
            <template #default="scope">
              {{ scope.row.userType == 1 ? '学生' : scope.row.userType == 2 ? '人员' : '全部' }}
            </template>
          </el-table-column>
          <el-table-column prop="type" label="应用类型" width="80">
            <template #default="scope">
              {{ scope.row.type == 1 ? 'PC应用' : scope.row.type == 2 ? '手机应用' : '全部' }}
            </template>
          </el-table-column>
          <el-table-column prop="url" label="应用网址" />
          <el-table-column prop="background" label="路由地址" />
          <el-table-column prop="sortNum" label="排序号" width="80" />
          <el-table-column prop="isTop" label="置顶" width="80">
            <template #default="scope">
              {{ scope.row.isTop == 1 ? '非置顶' : scope.row.isTop == 2 ? '置顶' : '其他' }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="apiKey" label="apiKey" /> -->
          <!-- <el-table-column prop="apiSecret" label="apiSecret" /> -->
          <el-table-column label="操作" width="140px">
            <template #default="scope">
              <el-button class="edit_btn" size="small" type="success" @click="editApp(scope.row)">编辑</el-button>
              <el-button class="del_btn" size="small" type="danger" @click="deleteApp(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <template v-slot:empty>
            <el-empty description="暂无数据"></el-empty>
          </template>
        </el-table>
        <el-config-provider :locale="locale">
          <el-pagination background="white" :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
            @current-change="changeCurrentPage">
          </el-pagination>
        </el-config-provider>
      </div>
      <bottomView />

    </div>
  </el-main>
</template>
<script>
import addAppView from "@/components/baseInfo/addApp.vue";
import bottomView from "@/components/bottom.vue";

import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";

import { httpService } from "@/utils/httpService";
import { Picture as IconPicture } from "@element-plus/icons-vue";
export default {
  name: "appListView",
  data() {
    return {
      list: [],
      pageSize: 10,
      page: 1,
      total: 0,
      pageCount: 0,
      form: {
        appName: "",
        type: "",
        userType: '',
      },
    };
  },
  mounted() {
    this.getListApp();
  },

  components: {
    addAppView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    bottomView,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  methods: {
    add() {
      this.$refs.addAppView.dialogFormVisible = true;
    },
    editApp(obj) {
      this.$refs.addAppView.getAppDataById(obj.id);
      this.$refs.addAppView.dialogFormVisible = true;
    },
    deleteApp(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService("/javaApi/appInfo/deleteById", "id=" + id, "post").then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.getListApp();
            } else {
              ElMessage.error(data.message);
            }
          });
        })
        .catch(() => { });
    },
    onSubmit() {
      this.getListApp();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.getListApp();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getListApp();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getListApp() {
      httpService(
        "/javaApi/appInfo/list/" + this.page + "/" + this.pageSize,
        {
          appName: this.form.appName,
          type: this.form.type ? this.form.type : this.form.type = "",
          userType: this.form.userType ? this.form.userType : this.form.userType = ""
        },
        "get"
      ).then((data) => {
        this.list = data.object.list;
        this.total = data.object.total;
        this.pageCount = data.object.pages;
      });
    },
  },
};
</script>

<style>
.app-table {
  background: #eff3f5;
  padding: 5px 10px 10px;
}

.app-search {}

.el-main {
  padding: 0px;
}

.app-separator {
  padding-top: 10px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px;
  min-height: calc(100vh - 216px);
  text-align: left;
  margin-bottom: 10px;

}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}



.edit_btn {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}
</style>
